package cn.zw.msmp.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zw.msmp.dao.ProductDao;
import cn.zw.msmp.pojo.ProductInfo;
import cn.zw.msmp.service.IProductService;

@Service("productService")
public class ProductServiceImpl implements IProductService{

	private static final Logger log = LoggerFactory.getLogger(ProductServiceImpl.class);
	
	@Autowired
	private ProductDao productDao;
	
	@Override
	public void batchInsert(List<ProductInfo> list) {
		try {
			productDao.batchInsert(list);
		} catch (Exception e) {
			log.error("batch insert product info error", e);
		}
		
	}

	@Override
	public ProductInfo getProduct(Integer id) {
		try {
			if(null != id) {
				return productDao.getCustomer(id);
			}
		} catch (Exception e) {
			log.error("get product by id error, id="+id, e);
		}
		return null;
	}

	@Override
	public void insert(ProductInfo productInfo) {
		try {
			productDao.insert(productInfo);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("insert product info error", e);
		}
		
	}

	@Override
	public PageInfo<ProductInfo> queryProducts(ProductInfo info, int pageIndex, int pageSize) {
		try {
			if(null != info) {
				PageHelper.startPage(pageIndex, pageSize);
				List<ProductInfo> list = productDao.queryProducts(info);
				return new PageInfo<ProductInfo>(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("get product by id error, info="+info.toString(), e);
		}
		return null;
	}

	@Override
	public void delete(List<Integer> ids) {
		try {
			if(null != ids && ids.size() > 0) {
				productDao.delete(ids);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("delete product info error", e);
		}
	}

	@Override
	public void update(ProductInfo productInfo) {
		try {
			if(null != productInfo) {
				productDao.update(productInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("update product error", e);
		}
	}
	
}
